package com.ngsky.ana.utils;

import net.coobird.thumbnailator.Thumbnails;

import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;

import static com.ngsky.ana.server.AnalyzeServer.COLOR_SIZE;
import static com.ngsky.ana.server.AnalyzeServer.fd;

/**
 * @Author syx
 * @Desc
 * @Create 2020/6/14 15:41
 */
public class ImageUtil {
    /**
     * 图片等比压缩处理
     */
    public static BufferedImage thumbnail(BufferedImage buff, int maxWidth, int maxHeight) {
        try {
            if (null != buff) {
                int width = buff.getWidth();
                int height = buff.getHeight();
                if (width > maxWidth || height > maxHeight) {
                    return Thumbnails.of(buff).size(maxWidth, maxHeight).asBufferedImage();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("thumbnail image failed! error: " + e.getMessage());
        }
        // 返回原图片
        return buff;
    }

    /**
     * 读取数据
     */
    public static String[] readDb(String filePath) {
        try {
            File file = new File(filePath);
            if (!file.exists() || !file.isFile()) {
                return null;
            }
            //简写如下
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(filePath), StandardCharsets.UTF_8));
            String line = "";
            String[] arrs = null;
            String[] ans = new String[COLOR_SIZE];
            int i = 1;
            while ((line = br.readLine()) != null) {
                // 从第三行开始读
                if (i >= 3) {
                    ans[i - 3] = line;
                }
                i++;
            }
            br.close();
            return ans;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 写数据
     */
    public static void writeDb(String filePath, int count, int curIndex, String imgFile, double[] color) {
        try {
            File saveFile = new File(filePath);

            // 索引: 12-0
            FileOutputStream fos = new FileOutputStream(saveFile);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
            bw.write("" + count + "-" + curIndex);
            bw.newLine();

            bw.write(imgFile);
            bw.newLine();

            for (int i = 0; i < color.length; i++) {
                String cv = fd(color[i] * 100) + "%";
                cv = "0%".equals(cv) ? "0" : cv;
                bw.write(cv);
                bw.newLine();
            }
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件名称
     */
    public static String getFileName(String filePath) {
        if (null == filePath || "".equals(filePath)) return "";
        String[] temp = filePath.split("\\\\");
        if (temp.length == 0) return filePath;
        return temp[temp.length - 1];
    }
}
